/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ws.converter;

import entidade.persistencia.EntradaSaida;
import java.net.URI;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;
import entidade.persistencia.FolhaPagamento;
import entidade.persistencia.Manutencao;
import entidade.persistencia.Venda;
import java.util.Collection;
import entidade.persistencia.Compra;
import java.util.List;

/**
 *
 * @author victorinox
 */

@XmlRootElement(name = "entradaSaida")
public class EntradaSaidaConverter {
    private EntradaSaida entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of EntradaSaidaConverter */
    public EntradaSaidaConverter() {
        entity = new EntradaSaida();
    }

    /**
     * Creates a new instance of EntradaSaidaConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public EntradaSaidaConverter(EntradaSaida entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getEntradaSaidaId() + "/").build() : uri;
        this.expandLevel = expandLevel;
        getVendaCollection();
        getFolhaPagamentoCollection();
        getManutencaoCollection();
        getCompraCollection();
    }

    /**
     * Creates a new instance of EntradaSaidaConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public EntradaSaidaConverter(EntradaSaida entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for entradaSaidaId.
     *
     * @return value for entradaSaidaId
     */
    @XmlElement
    public Integer getEntradaSaidaId() {
        return (expandLevel > 0) ? entity.getEntradaSaidaId() : null;
    }

    /**
     * Setter for entradaSaidaId.
     *
     * @param value the value to set
     */
    public void setEntradaSaidaId(Integer value) {
        entity.setEntradaSaidaId(value);
    }

    /**
     * Getter for dataOperacao.
     *
     * @return value for dataOperacao
     */
    @XmlElement
    public Date getDataOperacao() {
        return (expandLevel > 0) ? entity.getDataOperacao() : null;
    }

    /**
     * Setter for dataOperacao.
     *
     * @param value the value to set
     */
    public void setDataOperacao(Date value) {
        entity.setDataOperacao(value);
    }

    /**
     * Getter for dataVencimento.
     *
     * @return value for dataVencimento
     */
    @XmlElement
    public Date getDataVencimento() {
        return (expandLevel > 0) ? entity.getDataVencimento() : null;
    }

    /**
     * Setter for dataVencimento.
     *
     * @param value the value to set
     */
    public void setDataVencimento(Date value) {
        entity.setDataVencimento(value);
    }

    /**
     * Getter for quantidadeParcela.
     *
     * @return value for quantidadeParcela
     */
    @XmlElement
    public Integer getQuantidadeParcela() {
        return (expandLevel > 0) ? entity.getQuantidadeParcela() : null;
    }

    /**
     * Setter for quantidadeParcela.
     *
     * @param value the value to set
     */
    public void setQuantidadeParcela(Integer value) {
        entity.setQuantidadeParcela(value);
    }

    /**
     * Getter for parcelaAtual.
     *
     * @return value for parcelaAtual
     */
    @XmlElement
    public Integer getParcelaAtual() {
        return (expandLevel > 0) ? entity.getParcelaAtual() : null;
    }

    /**
     * Setter for parcelaAtual.
     *
     * @param value the value to set
     */
    public void setParcelaAtual(Integer value) {
        entity.setParcelaAtual(value);
    }

    /**
     * Getter for valor.
     *
     * @return value for valor
     */
    @XmlElement
    public Float getValor() {
        return (expandLevel > 0) ? entity.getValor() : null;
    }

    /**
     * Setter for valor.
     *
     * @param value the value to set
     */
    public void setValor(Float value) {
        entity.setValor(value);
    }

    /**
     * Getter for vendaCollection.
     *
     * @return value for vendaCollection
     */
    @XmlElement
    public VendasConverter getVendaCollection() {
        if (expandLevel > 0) {
            if (entity.getVendaCollection() != null) {
                return new VendasConverter(entity.getVendaCollection(), uri.resolve("vendaCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for vendaCollection.
     *
     * @param value the value to set
     */
    public void setVendaCollection(VendasConverter value) {
        entity.setVendaCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Getter for folhaPagamentoCollection.
     *
     * @return value for folhaPagamentoCollection
     */
    @XmlElement
    public FolhaPagamentoesConverter getFolhaPagamentoCollection() {
        if (expandLevel > 0) {
            if (entity.getFolhaPagamentoCollection() != null) {
                return new FolhaPagamentoesConverter(entity.getFolhaPagamentoCollection(), uri.resolve("folhaPagamentoCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for folhaPagamentoCollection.
     *
     * @param value the value to set
     */
    public void setFolhaPagamentoCollection(FolhaPagamentoesConverter value) {
        entity.setFolhaPagamentoCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Getter for manutencaoCollection.
     *
     * @return value for manutencaoCollection
     */
    @XmlElement
    public ManutencaoesConverter getManutencaoCollection() {
        if (expandLevel > 0) {
            if (entity.getManutencaoCollection() != null) {
                return new ManutencaoesConverter(entity.getManutencaoCollection(), uri.resolve("manutencaoCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for manutencaoCollection.
     *
     * @param value the value to set
     */
    public void setManutencaoCollection(ManutencaoesConverter value) {
        entity.setManutencaoCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Getter for compraCollection.
     *
     * @return value for compraCollection
     */
    @XmlElement
    public ComprasConverter getCompraCollection() {
        if (expandLevel > 0) {
            if (entity.getCompraCollection() != null) {
                return new ComprasConverter(entity.getCompraCollection(), uri.resolve("compraCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for compraCollection.
     *
     * @param value the value to set
     */
    public void setCompraCollection(ComprasConverter value) {
        entity.setCompraCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the EntradaSaida entity.
     *
     * @return an entity
     */
    @XmlTransient
    public EntradaSaida getEntity() {
        if (entity.getEntradaSaidaId() == null) {
            EntradaSaidaConverter converter = UriResolver.getInstance().resolve(EntradaSaidaConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved EntradaSaida entity.
     *
     * @return an resolved entity
     */
    public EntradaSaida resolveEntity(EntityManager em) {
        List<Venda> vendaCollection = entity.getVendaCollection();
        List<Venda> newvendaCollection = new java.util.ArrayList<Venda>();
        for (Venda item : vendaCollection) {
            newvendaCollection.add(em.getReference(Venda.class, item.getVendaId()));
        }
        entity.setVendaCollection(newvendaCollection);
        List<FolhaPagamento> folhaPagamentoCollection = entity.getFolhaPagamentoCollection();
        List<FolhaPagamento> newfolhaPagamentoCollection = new java.util.ArrayList<FolhaPagamento>();
        for (FolhaPagamento item : folhaPagamentoCollection) {
            newfolhaPagamentoCollection.add(em.getReference(FolhaPagamento.class, item.getFolhaPagamentoId()));
        }
        entity.setFolhaPagamentoCollection(newfolhaPagamentoCollection);
        List<Manutencao> manutencaoCollection = entity.getManutencaoCollection();
        List<Manutencao> newmanutencaoCollection = new java.util.ArrayList<Manutencao>();
        for (Manutencao item : manutencaoCollection) {
            newmanutencaoCollection.add(em.getReference(Manutencao.class, item.getManutencaoId()));
        }
        entity.setManutencaoCollection(newmanutencaoCollection);
        List<Compra> compraCollection = entity.getCompraCollection();
        List<Compra> newcompraCollection = new java.util.ArrayList<Compra>();
        for (Compra item : compraCollection) {
            newcompraCollection.add(em.getReference(Compra.class, item.getCompraId()));
        }
        entity.setCompraCollection(newcompraCollection);
        return entity;
    }
}
